﻿using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using iemtm.WeChat.Core.Util;
using Quartz;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace iemtm.WeChat.Core.Quartz.Job
{
    [DisallowConcurrentExecution]//前一次任务未执行完成时不触发下次任务
    public class CheckGeneratorRunTime : IJob
    {
        
        public Task Execute(IJobExecutionContext context)
        {
            return Task.Run(() =>
            {
                try
                {
                    string UserCode = "";
                    string GenNum = "";
                    //找出所有需要保养的油机
                    string sql = $"SELECT g.GensetNum,g.DTUID,u.UserCode FROM dbo.GeneratorRunTime g LEFT JOIN dbo.UserAccount u ON g.CompanyCode=u.CompanyCode WHERE (g.RunTime-g.MaintenanceTime)>100 AND g.PrjCode='0113' AND u.RoleCode='powermanGZ'";
                    DataTable DT = (sql).SqlQuery();
                    if (DT.Rows.Count > 0)
                    {
                        foreach (DataRow dr in DT.Rows)
                        {
                            UserCode = dr["UserCode"].ToString();
                            GenNum = dr["GensetNum"].ToString();
                            if ((UserCode != null && UserCode != "") && (GenNum != null && GenNum != ""))
                            {
                                WeiXinPush.SendGenRunMsg(UserCode, GenNum);
                            }

                        }
                    }
                }
                catch (Exception e)
                {
                    Db.GetRepository<Log>().Insert(new Log() { LogName = "油机保养提醒推送失败", LogDetails = e.ToString(), CreateTime = DateTime.Now });
                }
            });
        }
    }
}
